package BioClusterAction;

import BioClusterForm.ListIterationForm;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ListIterationAction extends Action {
    /**This is the main action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
                                 HttpServletRequest request, 
                                 HttpServletResponse response) throws IOException, 
                                                                      ServletException {
        ListIterationForm listIterationForm = (ListIterationForm)form;
        String fileNamesInAsc[] = (String[]) request.getSession().getAttribute("fileNamesInAsc");
        int offset = Integer.parseInt((String) request.getSession().getAttribute("offset"));
        System.out.println();
        if(listIterationForm.getClusterControl().equals("Back")) {
            request.getSession().setAttribute("fileNameFromAction",fileNamesInAsc[offset-2]);
            request.getSession().setAttribute("offset",String.valueOf(offset-1));
        } else if(listIterationForm.getClusterControl().equals("Cluster Further")) {
            request.getSession().setAttribute("fileNameFromAction",fileNamesInAsc[offset]);
            request.getSession().setAttribute("offset",String.valueOf(offset+1));
        }
        return mapping.findForward("success");
    }
}
